package com.jwj.controller;

import com.jwj.pojo.ImgResult;
import com.jwj.pojo.Picture;
import com.jwj.service.PictureService;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.UUID;

@RestController
public class PictureController {
    @Resource
    private PictureService pictureService;
//    添加图片
    @RequestMapping(value = "/touxiang")
    public String PictureAdd2(@RequestParam("img_id") String img_id,@RequestParam("img_url") MultipartFile img_url) throws IOException{
//        Integer id = Integer.valueOf(img_id);
//        System.out.println(id);
        BigInteger id=new BigInteger(img_id);
        System.out.println(img_url);
        String path = System.getProperty("user.dir");//获取项目相对路径
        String imagePath =path+"/src//main/resources/static/img/";
        File file = new File(imagePath);
        //判断文件夹是否存在
        if (!file.exists()){
//            不存在则创建文件夹
            file.mkdirs();
        }
        //获取jar包所在目录
//        ApplicationHome h =new ApplicationHome(getClass());
//        File jarF = h.getSource();
//        在jar包所在目录下生成upload文件夹用来存储上传的图片
//        String dirPath = jarF.getParentFile().toString()+"/upload/";
//        System.out.println(dirPath);
//        File filePath = new File(dirPath);
//        if (!filePath.exists()){
//            不存在则创建文件夹
//            filePath.mkdirs();
//        }
        //获取图片名
        String fileName = img_url.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-","");
        String imageFilePath = imagePath + uuid + fileType;
        img_url.transferTo(new File(imageFilePath));
        int s = pictureService.PictureAdd2(id,"/img/"+uuid+fileType,fileName,"图片");
        System.out.println(s);
        return "/img/"+uuid+fileType;
    }

//    添加图片
    private String serverPath ="http://localhost:8081/img/";
@RequestMapping(value = "/file")
public ImgResult PictureAdd(@RequestParam("img_url") MultipartFile img_url) throws IOException{
    System.out.println(img_url);
    String path = System.getProperty("user.dir");//获取项目相对路径
    String imagePath =path+"/src//main/resources/static/img/";
    try {
        File file = new File(imagePath);
        //判断文件夹是否存在
        if (!file.exists()){
//            不存在则创建文件夹
            file.mkdirs();
        }
        //获取图片名
        String fileName = img_url.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-","");
        String imageFilePath = imagePath + uuid + fileType;
        img_url.transferTo(new File(imageFilePath));
        imageFilePath = serverPath+uuid+fileType;
        String [] strings = {imageFilePath};//返回给前端的图片地址
        int s = pictureService.PictureAdd("/img/"+uuid+fileType,fileName,"图片");
        System.out.println(s);
        return ImgResult.suc(strings);
    }catch (IOException e){
        e.printStackTrace();
    }
    return ImgResult.fai();

}
//    查找图片
//    @RequestMapping(value = "/all")
//    public List<Picture> PictureAll(){
//        List<Picture> pictures = pictureService.PictureAll();
//        System.out.println(pictures);
//        return pictures;
//    }
//    数据库删除图片
    @RequestMapping(value = "/delete")
    public int PictureDelete(BigInteger img_id){
        System.out.println(img_id);
        int i = pictureService.PictureDelete(img_id);
        return i;
    }
//    取消上传图片
    @RequestMapping(value = "/cancels")
    public String cancels(String path){
        System.out.println(path);
        String resultInfo = null;
        String way = System.getProperty("user.dir");
        String imagePath =way+"/src//main/resources/static/";
//        System.out.println(imagePath);
//        System.out.println(path);
        String fileType = path.substring(path.lastIndexOf("."));
//        System.out.println(fileType);
        if (fileType.equals("png")||fileType.equals("jpg"))
        {
            resultInfo = "选择图片";
        }else {
            String sb=imagePath+path;
//            System.out.println(sb);
            File file=new File(sb);
            if (file.exists()){
                if (file.delete()){
                    resultInfo = "删除成功";
                }else {
                    resultInfo="删除失败";
                }
            }else {
                resultInfo="路径不存在";
            }
        }
        return resultInfo;
    }
}

